<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.IOException"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="java.net.HttpURLConnection"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.MalformedURLException"%>
<%@page import="java.net.URL"%>
<%!
	private final static HttpURLConnection createCnnection(String url,
			String method) throws MalformedURLException, IOException {
		HttpURLConnection conn = (HttpURLConnection) new URL(url)
				.openConnection();
		conn.setUseCaches(false);//设置用户缓存
		conn.setConnectTimeout(2000);//与请求网址的服务器建立连接的超时时间
		conn.setRequestMethod(method);
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("Accept-Charset", "UTF-8");
		conn.setRequestProperty("Content-Type", "textml;charset=UTF-8");
		conn.setRequestProperty("user-agent",
				"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		return conn;
	}

	public static final String getReturnToken(String url, String method,
			String params) {
		StringBuffer message = new StringBuffer();
		BufferedReader reader = null;
		PrintWriter print = null;
		try {
			HttpURLConnection conn = createCnnection(url, method);// 建立链接对象
			conn.setReadTimeout(2000);//设置从主机读取数据超时
			conn.setDoInput(true);
			conn.setDoOutput(true);
			if (params != null) {
				conn.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded;charset=UTF-8");// 设置参数类型
				print = new PrintWriter(conn.getOutputStream());// 携带消息参数
				print.print(params);
				print.flush();
			}
			conn.connect();// 发送请求
			reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "UTF-8"));// 读取服务端响应数据
			readMessage(reader, message);
			conn.disconnect();// 断开连接
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (print != null)
					print.close();
				if (reader != null)
					reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if ("".equals(message.toString())) {
			return null;
		} else {
			return message.toString();
		}
	}

	private static void readMessage(BufferedReader reader, StringBuffer message) {
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				message.append(line);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
%>
